home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / EARTHQUA.ZIP / EARTHQUA.CPP next >
C/C++ Source or Header  |  1995-02-04  |  12KB  |  528 lines

  1. // EARTHQUA.CPP - The main program for the Earthquake Damage Prevention
  2. // program. Displays the menus.
  3. // Created by Misha Koshelev.
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8. #include <dos.h>
  9. #include <process.h>
  10. #include <graphics.h>
  11. #include "svga256.h"
  12. #include "tutor.h"
  13.  
  14. // Definitons
  15. #define TUTORIAL 0
  16. #define SIMULATION 1
  17.  
  18. #define INTRO 0
  19. #define EQSIM 1
  20. #define BMSIM 2
  21.  
  22. #define QUIT 255
  23.  
  24. #define UP_ARROW 72
  25. #define DOWN_ARROW 80
  26.  
  27. void eqsimmain(void);
  28.  
  29. // Function definitons
  30. int huge Svga256Detect(void);
  31. void drawtitle(void);
  32. void loadpal(char *);
  33. void hrainbow(int, int, int, int, int, int, int, int);
  34. void vrainbow(int, int, int, int, int, int, int, int);
  35. void highlightitem(int, int, int, int, int, int);
  36. void unhighlightitem(int, int, int, int, int, int);
  37. void nextitem(int, int);
  38. void previtem(int, int);
  39. int mainmenu(void);
  40. int tutormenu(void);
  41.  
  42. int page;
  43.  
  44. void main(int argc, char *argv[])
  45. {
  46.    int gd = DETECT, gm, errorcode;
  47.    int y = 10;
  48.    int ret, done=0, tdone = 0;
  49.  
  50.    // Set up graphics mode
  51.  
  52.    // Install the Super VGA 256 color driver
  53.    installuserdriver("Svga256", Svga256Detect);
  54.  
  55.    // read the result of installation
  56.    errorcode = graphresult();
  57.  
  58.    if (errorcode != grOk)  // an error occurred
  59.    {
  60.       printf("Graphics error: %s\n", grapherrormsg(errorcode));
  61.       printf("Press any key to quit...");
  62.       getch();
  63.       exit(1);
  64.    }
  65.  
  66.    // Initalize graphics mode
  67.    initgraph(&gd, &gm, "");
  68.  
  69.    // read result of initialization
  70.    errorcode = graphresult();
  71.  
  72.    if (errorcode != grOk)  // an error occurred
  73.    {
  74.       printf("Graphics error: %s\n", grapherrormsg(errorcode));
  75.       printf("Press any key to quit...");
  76.       getch();
  77.       exit(1);
  78.    }
  79.  
  80.    // Load the palette
  81.    loadpal("std.pal");
  82.  
  83.    // Display the title screen
  84.    setactivepage(1);
  85.    page = 1;
  86.    cleardevice();
  87.  
  88.    setcolor(WHITE);
  89.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 7);
  90.    outtextxy(10, 10,"EARTHQUAKE");
  91.  
  92.    y += textheight("EARTHQUAKE") + 10;
  93.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  94.    outtextxy(170, y, "DAMAGE PREVENTION");
  95.  
  96.    y += textheight("DAMAGE PREVENTION") + 10;
  97.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  98.    outtextxy(170, y, "Press any key to continue...");
  99.  
  100.    y += textheight("Press any key to continue...") + 10;
  101.    hrainbow(1,1,640,y,WHITE,1,96,111);
  102.  
  103.    setvisualpage(1);
  104.  
  105.    while(!kbhit());
  106.    getch();
  107.  
  108.    setfillstyle(SOLID_FILL, BLACK);
  109.    bar(1, y-20, 640, y);
  110.  
  111.    while (!done)
  112.    {
  113.       ret = mainmenu();
  114.       if (ret == QUIT)
  115.      done = 1;
  116.       if (ret == TUTORIAL)
  117.       {
  118.         setfillstyle(SOLID_FILL, BLACK);
  119.         bar(1, 120, 640, 480);
  120.         tdone = 0;
  121.         while (!tdone)
  122.         {
  123.           ret = tutormenu();
  124.           if (ret == QUIT)
  125.             tdone = 1;
  126.           if (ret == INTRO)
  127.           {
  128.             tutorintro();
  129.             // Draw the title screen
  130.             drawtitle();
  131.           }
  132.           if (ret == EQSIM)
  133.           {
  134.             tutoreqsim();
  135.             // Draw the title screen
  136.             drawtitle();
  137.           }
  138.           if (ret == BMSIM)
  139.           {
  140.             tutorbmsim();
  141.             // Draw the title screen
  142.             drawtitle();
  143.           }
  144.         }
  145.         setfillstyle(SOLID_FILL, BLACK);
  146.         bar(1, 120, 640, 480);
  147.      }
  148.      if (ret == SIMULATION)
  149.       {
  150.         setviewport(0,0,639,479,1);
  151.         settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  152.         setcolor(WHITE);
  153.         eqsimmain();
  154.         cleardevice();
  155.         // Draw the title screen
  156.         drawtitle();
  157.       }
  158.    }
  159.    closegraph();
  160. }
  161.  
  162. // Returns what video mode to use.
  163. //
  164. int huge Svga256Detect(void)
  165. {
  166.    return SVGA640x480x256;
  167. }
  168.  
  169. // Draws the title screen in the appropriate page and displays it.
  170. //
  171. void drawtitle(void)
  172. {
  173.    int y;
  174.  
  175.    // Display the title screen
  176.    if (page == 1)
  177.      page = 0;
  178.    if (page == 0)
  179.      page = 1;
  180.    setactivepage(page);
  181.    cleardevice();
  182.  
  183.    y = 10;
  184.    setcolor(WHITE);
  185.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 7);
  186.    outtextxy(10, 10,"EARTHQUAKE");
  187.  
  188.    y += textheight("EARTHQUAKE") + 10;
  189.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  190.    outtextxy(170, y, "DAMAGE PREVENTION");
  191.  
  192.    y += textheight("DAMAGE PREVENTION") + 10;
  193.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  194.    hrainbow(1,1,640,y,WHITE,1,96,111);
  195.  
  196.    setvisualpage(page);
  197. }
  198.  
  199. // Loads the palette from a raw palette file.
  200. //
  201. void loadpal(char *fn)
  202. {
  203.    int color;
  204.    int r, g, b;
  205.    FILE *pal;
  206.  
  207.    pal = fopen(fn, "rb");
  208.  
  209.    if (pal == NULL)
  210.    {
  211.       closegraph();
  212.       printf("Error: Could not open palette file %s", fn);
  213.       exit(1);
  214.    }
  215.    for (color = 0; color < 256; color++)
  216.    {
  217.       fread(&r, 1, 1, pal);
  218.       fread(&g, 1, 1, pal);
  219.       fread(&b, 1, 1, pal);
  220.       setrgbpalette(color, r, g, b);
  221.    }
  222.    fclose(pal);
  223. };
  224.  
  225. // Draws a horizontal "rainbow" on every pixel between (x1, y1) and
  226. // (x2, y2) of keycolor, where each color
  227. // takes barsize pixels, and the colors start from startc and end
  228. // with endc.
  229. //
  230. void hrainbow(int x1, int y1, int x2, int y2, int keyc, int barsize,
  231.          int startc, int endc)
  232. {
  233.    int currcolor = startc;
  234.    int ccpix = barsize;
  235.  
  236.    for (int k=y1; k<y2+1; k++)
  237.    {
  238.       for (int l=x1; l<x2+1; l++)
  239.       {
  240.      if (getpixel(l, k) == keyc)
  241.      {
  242.         if (ccpix == 0)
  243.         {
  244.            ccpix = barsize;
  245.            currcolor++;
  246.            if (currcolor > endc)
  247.           currcolor = startc;
  248.         }
  249.         putpixel(l, k, currcolor);
  250.         ccpix--;
  251.      }
  252.       }
  253.       ccpix = barsize;
  254.       currcolor = startc;
  255.    }
  256. }
  257.  
  258. // Draws a vertical "rainbow" on every pixel between (x1, y1) and
  259. // (x2, y2) of keycolor, where each color
  260. // takes barsize pixels, and the colors start from startc and end
  261. // with endc.
  262. //
  263. void vrainbow(int x1, int y1, int x2, int y2, int keyc, int barsize,
  264.          int startc, int endc)
  265. {
  266.    int currcolor = startc;
  267.    int ccpix = barsize;
  268.  
  269.    for (int k=x1; k<x2+1; k++)
  270.    {
  271.       for (int l=y1; l<y2+1; l++)
  272.       {
  273.      if (getpixel(k, l) == keyc)
  274.      {
  275.         if (ccpix == 0)
  276.         {
  277.            ccpix = barsize;
  278.            currcolor++;
  279.            if (currcolor > endc)
  280.           currcolor = startc;
  281.         }
  282.         putpixel(k, l, currcolor);
  283.         ccpix--;
  284.      }
  285.       }
  286.       ccpix = barsize;
  287.       currcolor = startc;
  288.    }
  289. }
  290.  
  291. // Highlights an unselected item with a specific color, in which the
  292. // text is a specific color
  293. void highlightitem(int x, int y, int x2, int y2, int hc, int tc)
  294. {
  295.    int i,j;
  296.  
  297.    for (i=y; i<y2+1; i++)
  298.    {
  299.       for (j=x; j<x2+1; j++)
  300.       {
  301.       if (getpixel(j,i) != tc)
  302.          putpixel(j,i,hc);
  303.       }
  304.    }
  305. }
  306.  
  307. // Unhighlights an unselected item with a specific color, in which the
  308. // text is a specific color
  309. void unhighlightitem(int x, int y, int x2, int y2, int hc, int tc)
  310. {
  311.    int i,j;
  312.  
  313.    for (i=y; i<y2+1; i++)
  314.    {
  315.       for (j=x; j<x2+1; j++)
  316.       {
  317.       if (getpixel(j,i) != tc)
  318.          putpixel(j,i,hc);
  319.       }
  320.    }
  321. }
  322.  
  323. // Returns the next item if ci is the current item and maxi is the number
  324. // of items
  325. void nextitem(int *ci, int maxi)
  326. {
  327.    if (*ci == maxi-1)
  328.       *ci = 0;
  329.    else
  330.       *ci+=1;
  331. }
  332.  
  333. // Returns the previous item if ci is the current item and maxi is the number
  334. // of items
  335. void previtem(int *ci, int maxi)
  336. {
  337.    if (*ci == 0)
  338.       *ci = maxi-1;
  339.    else
  340.       *ci-=1;
  341. }
  342.  
  343. // Displays the main menu. Returns the number of the item selected.
  344. //
  345. int mainmenu(void)
  346. {
  347.    int x[2], y[2], x2[2], y2[2];
  348.    int curritem = 0;
  349.    int done = 0;
  350.    char ch;
  351.  
  352.    // Write the instructions
  353.    settextstyle(SOLID_FILL, HORIZ_DIR, 2);
  354.    setcolor(WHITE);
  355.  
  356.    outtextxy(70,130, "Use the arrow keys to choose an item,  ");
  357.    outtextxy(70,150, "and press the <ENTER> key to select it.");
  358.    outtextxy(70,170, "     Press the <ESC> key to quit.      ");
  359.  
  360.    // Draw a bluish rectangle
  361.    setcolor(143);
  362.    rectangle(10,250,630,469);
  363.  
  364.    // Write text in a red color
  365.    settextstyle(SOLID_FILL, HORIZ_DIR, 4);
  366.    setcolor(32);
  367.  
  368.    x[0] = 250; y[0] = 320;
  369.    outtextxy(x[0],y[0],"Tutorial");
  370.    x[0] -= 10;
  371.    x2[0] = x[0] + textwidth("Tutorial") + 10;
  372.    y2[0] = y[0] + textheight("Tutorial") + 10;
  373.  
  374.    x[1] = 230; y[1] = 370;
  375.    outtextxy(x[1],y[1],"Simulation");
  376.    x[1] -= 10;
  377.    x2[1] = x[1] + textwidth("Simulation") + 10;
  378.    y2[1] = y[1] + textheight("Simulation") + 10;
  379.  
  380.    // Highlight in a greenish color
  381.    highlightitem(x[curritem], y[curritem], x2[curritem], y2[curritem],
  382.      96, 32);
  383.  
  384.    while (!done)
  385.    {
  386.       ch = getch();
  387.       if (ch != 0 && ch != 13 && ch != 27)
  388.       {
  389.      sound(440);
  390.      delay(100);
  391.      nosound();
  392.       }
  393.       if (ch == 13)
  394.      return curritem;
  395.       if (ch == 27)
  396.      return QUIT;
  397.       if (ch == 0)
  398.       {
  399.      ch = getch();
  400.      if (ch != UP_ARROW && ch != DOWN_ARROW)
  401.      {
  402.         sound(440);
  403.         delay(100);
  404.         nosound();
  405.      }
  406.      if (ch == UP_ARROW)
  407.      {
  408.         unhighlightitem(x[curritem], y[curritem],
  409.                 x2[curritem], y2[curritem],
  410.                 0, 32);
  411.         previtem(&curritem, 2);
  412.         highlightitem(x[curritem], y[curritem],
  413.               x2[curritem], y2[curritem],
  414.               96, 32);
  415.      }
  416.      if (ch == DOWN_ARROW)
  417.      {
  418.         unhighlightitem(x[curritem], y[curritem],
  419.                 x2[curritem], y2[curritem],
  420.                 0, 32);
  421.         nextitem(&curritem, 2);
  422.         highlightitem(x[curritem], y[curritem],
  423.               x2[curritem], y2[curritem],
  424.               96, 32);
  425.      }
  426.       }
  427.    }
  428. }
  429.  
  430. // Displays the tutorial menu. Returns the number of the item selected.
  431. //
  432. int tutormenu(void)
  433. {
  434.    int x[3], y[3], x2[3], y2[3];
  435.    int curritem = 0;
  436.    int done = 0;
  437.    char ch;
  438.  
  439.    // Write the instructions
  440.    settextstyle(SOLID_FILL, HORIZ_DIR, 2);
  441.    setcolor(WHITE);
  442.  
  443.    outtextxy(70,130, "Use the arrow keys to choose an item,  ");
  444.    outtextxy(70,150, "and press the <ENTER> key to select it.");
  445.    outtextxy(50,170, "Press the <ESC> key go back to the main menu");
  446.  
  447.    // Draw a bluish rectangle
  448.    setcolor(143);
  449.    rectangle(10,250,630,469);
  450.  
  451.    // Write text in a red color
  452.    settextstyle(SOLID_FILL, HORIZ_DIR, 4);
  453.    setcolor(32);
  454.  
  455.    x[0] = 218; y[0] = 260;
  456.    outtextxy(x[0],y[0],"Introduction");
  457.    x[0] -= 10;
  458.    x2[0] = x[0] + textwidth("Introduction") + 10;
  459.    y2[0] = y[0] + textheight("Introduction") + 10;
  460.  
  461.    x[1] = 130; y[1] = 300;
  462.    outtextxy(x[1],y[1],"How does the earthquake");
  463.    x[1] -= 10;
  464.    x2[1] = x[1] + textwidth("How does the earthquake") + 10;
  465.    y2[1] = y[1] + textheight("How does the earthquake") + 5;
  466.    outtextxy(x[1]+80, y2[1],"simulator work?");
  467.    y2[1] += textheight("simulator work?") + 5;
  468.  
  469.    x[2] = 130; y[2] = 380;
  470.    outtextxy(x[2],y[2],"How does the building");
  471.    x[2] -= 30;
  472.    x2[2] = x[2] + textwidth("movement simulator work?") + 10;
  473.    y2[2] = y[2] + textheight("How does the building") + 5;
  474.    outtextxy(x[2]+4, y2[2],"movement simulator work?");
  475.    y2[2] += textheight("movement simulator work?") + 5;
  476.  
  477.    // Highlight in a greenish color
  478.    highlightitem(x[curritem], y[curritem], x2[curritem], y2[curritem],
  479.      96, 32);
  480.  
  481.    while (!done)
  482.    {
  483.       ch = getch();
  484.       if (ch != 0 && ch != 13 && ch != 27)
  485.       {
  486.      sound(440);
  487.      delay(100);
  488.      nosound();
  489.       }
  490.       if (ch == 13)
  491.      return curritem;
  492.       if (ch == 27)
  493.      return QUIT;
  494.       if (ch == 0)
  495.       {
  496.      ch = getch();
  497.      if (ch != UP_ARROW && ch != DOWN_ARROW)
  498.      {
  499.         sound(440);
  500.         delay(100);
  501.         nosound();
  502.      }
  503.      if (ch == UP_ARROW)
  504.      {
  505.         unhighlightitem(x[curritem], y[curritem],
  506.                 x2[curritem], y2[curritem],
  507.                 0, 32);
  508.         previtem(&curritem, 3);
  509.         highlightitem(x[curritem], y[curritem],
  510.               x2[curritem], y2[curritem],
  511.               96, 32);
  512.      }
  513.      if (ch == DOWN_ARROW)
  514.      {
  515.         unhighlightitem(x[curritem], y[curritem],
  516.                 x2[curritem], y2[curritem],
  517.                 0, 32);
  518.         nextitem(&curritem, 3);
  519.         highlightitem(x[curritem], y[curritem],
  520.               x2[curritem], y2[curritem],
  521.               96, 32);
  522.      }
  523.       }
  524.    }
  525. }
  526.  
  527.  
  528.